﻿using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Web;

namespace Hugo
{
    class Translate
    {
        public static String translate(string input)
        {
            return translate(input, GlobalSettings.translation);
        }

        private static String start = "<span id=result_box";// class=\"short_text\"><span title=\"Communicate\" onmouseover=\"this.style.backgroundColor='#ebeff9'\" onmouseout=\"this.style.backgroundColor='#fff'\">";
        private static String middle = "onmouseout=\"this.style.backgroundColor='#fff'\">";
            //"<span id=result_box class=\"short_text\"><span title=\"Home\" onmouseover=\"this.style.backgroundColor='#ebeff9'\" onmouseout=\"this.style.backgroundColor='#fff'\">";
        private static String end = "</span>";

        public static String translate(string input,string languagePair)
        {
            if (languagePair.Equals("en|en")) { return input; }

            string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
            WebClient webClient = new WebClient();
            webClient.Encoding = System.Text.Encoding.UTF8;
            string result = webClient.DownloadString(url);

            int chopIndex = result.LastIndexOf(start);// + start.Length;
            string rest = result.Substring(chopIndex);
            chopIndex = rest.IndexOf(middle) + middle.Length;
            rest = rest.Substring(chopIndex);
            chopIndex = rest.IndexOf(end);
            rest = rest.Substring(0, chopIndex);

            //result = result.Substring(result.IndexOf("id=result_box") + 22, result.IndexOf("id=result_box") + 500);
            //result = result.Substring(0, result.IndexOf("</div"));
            return rest;
        }
    }
}


